<?php

namespace app\api\controller;

use app\BaseController;
use app\common\model\ChatGroupModel;
use app\common\model\ChatRecordModel;
use app\common\model\LiangBiaoModel;
use think\facade\Db;

class  Chat extends BaseController
{

    /**
     * @var  ChatGroupModel
     */
    private $groupModel;
    /**
     * @var  ChatRecordModel
     */
    private $recordModel;

    public function initialize()
    {
        $this->groupModel = new ChatGroupModel();
        $this->recordModel = new ChatRecordModel();
    }

    /**
     * 读取最近消息
     */
    public function index()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];

        $option = $this->option();
        $docId = $option['user']['account_id'] ? $option['user']['account_id'] : 0;
        $patientId = $this->request->param("id", 0);
        if (empty($patientId)) {
            $result['msg'] = 'id参数缺少';
            return $result;
        }
        $groupId = 0;
        $group = Db::table("gy_chat_group")
            ->where("doc_id", $docId)
            ->where("patient_id", $patientId)
            ->find();
        if (empty($group)) {
            $docInfo = Db::table("gy_account")
                ->where("account_id", $docId)
                ->find();
            $patientInfo = Db::table("gy_patient")
                ->where("id", $patientId)
                ->find();

            //新建group
            $insertData = [
                'patient_id' => $patientId,
                'patient_name' => $patientInfo['name'],
                'doc_id' => $docId,
                'doc_name' => $docInfo['nick_name'],
                "creat_time" => date("Y-m-d H:i:s")
            ];
            $insertResult = Db::table("gy_chat_group")->insertGetId($insertData);
            if ($insertResult) {
                $groupId = $insertResult;
            } else {
                return json($result);
            }
        } else {
            $groupId = $group['id'];
        }
        $groupRecord = Db::table("gy_chat_record")
            ->where('group_id', $groupId)
            ->order("creat_time", 'desc')
            ->limit(10)
            ->select()
            ->toArray();

        $result['status'] = 'ok';
        $result['msg'] = '查找成功';
        $result['data'] = [
            'groupId' => $groupId,
            'msg' => array_reverse($groupRecord)
        ];
        return json($result);
    }

    public function lastMsg()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $groupId = $this->request->param("id", 0);
        if (empty($groupId)) {
            $result['msg'] = 'id参数缺少';
            return $result;
        }

        $groupRecord = Db::table("gy_chat_record")
            ->where('group_id', $groupId)
            ->order("creat_time", 'desc')
            ->limit(10)
            ->select()
            ->toArray();

        $result['status'] = 'ok';
        $result['msg'] = '查找成功';
        $result['data'] = array_reverse($groupRecord);

        return json($result);
    }

    public function sendMsg()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $groupId = $this->request->param("gid", 0);
        if (empty($groupId)) {
            $result['msg'] = 'id参数缺少';
            return $result;
        }
        $msg = $this->request->param("msg", "");
        if (empty($msg)) {
            $result['msg'] = '请输入发送内容';
            return $result;
        }
        $msgType = $this->request->param("msg_type", 0);
        if (empty($msgType)) {
            $result['msg'] = 'type参数缺少';
            return $result;
        }
        $msgFrom = $this->request->param("msg_from", 2);
        if (empty($msgFrom)) {
            $result['msg'] = 'msg_from参数缺少';
            return $result;
        }
        $group = Db::table("gy_chat_group")
            ->where("id", $groupId)
            ->find();

        if (empty($group)) {
            $result['msg'] = '非法聊天';
            return $result;
        }

        $insertData = [
            'group_id' => $groupId,
            'msg' => $msg,
            'msg_type' => $msgType,
            'msg_from' => $msgFrom,
            "creat_time" => date("Y-m-d H:i:s")
        ];
        $resultId = Db::table("gy_chat_record")->insertGetId($insertData);
        if ($resultId) {
            //查找最后一个消息
            $lastMsg = Db::table("gy_chat_record")
                ->where('id', $resultId)
                ->find();
            //更新
            $updateData = [
                'msg' => $msg,
                'msg_type' => $msgType,
                'msg_from' => $msgFrom,
                "msg_time" => date("Y-m-d H:i:s")
            ];
            Db::table("gy_chat_group")
                ->where('id', $groupId)
                ->update($updateData);

            $result['status'] = 'ok';
            $result['msg'] = '发送成功';
            $result['data'] = $lastMsg;
        }
        return json($result);
    }

    public function docChat()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];

        $option = $this->option();
        $docId = $option['user']['account_id'] ? $option['user']['account_id'] : 0;
        $group = Db::table("gy_chat_group")
            ->where("doc_id", $docId)
            ->order("msg_time", 'desc')
            ->select();

        $result['status'] = 'ok';
        $result['msg'] = '查找成功';
        $result['data'] = $group;

        return json($result);
    }

    //检查是否登录
    private function checkLogin()
    {
        $result = [
            "status" => "noLogin",
            "data" => [],
            "msg" => "请登录"
        ];

        if ($this->user == null) {
            return json($result);
        }
    }
}